import 'dart:convert';

import 'package:flutter/foundation.dart';

class GoodsModel {
  int id;
  String title;
  String thumbnail;
  double originPrice;
  double price;

  ///副标题
  String subTitle;

  ///销售特性
  List<String>? salesTag = [];

  ///评价数量
  int evaluate;

  ///好评率
  int favourable;

  ///回头客
  int repeatCustomer;

  ///评分
  double score;

  GoodsModel({
    required this.id,
    required this.title,
    required this.thumbnail,
    required this.originPrice,
    required this.price,
    this.subTitle = '',
    this.salesTag,
    this.evaluate = 0,
    this.favourable = 0,
    this.repeatCustomer = 0,
    this.score = 0,
  });

  @override
  String toString() {
    return 'GoodsModel(id: $id, title: $title, thumbnail: $thumbnail, originPrice: $originPrice, price: $price, subTitle: $subTitle, salesTag: $salesTag, evaluate: $evaluate, favourable: $favourable, repeatCustomer: $repeatCustomer, score: $score)';
  }

  GoodsModel copyWith({
    int? id,
    String? title,
    String? thumbnail,
    double? originPrice,
    double? price,
    String? subTitle,
    List<String>? salesTag,
    int? evaluate,
    int? favourable,
    int? repeatCustomer,
    double? score,
  }) {
    return GoodsModel(
      id: id ?? this.id,
      title: title ?? this.title,
      thumbnail: thumbnail ?? this.thumbnail,
      originPrice: originPrice ?? this.originPrice,
      price: price ?? this.price,
      subTitle: subTitle ?? this.subTitle,
      salesTag: salesTag ?? this.salesTag,
      evaluate: evaluate ?? this.evaluate,
      favourable: favourable ?? this.favourable,
      repeatCustomer: repeatCustomer ?? this.repeatCustomer,
      score: score ?? this.score,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'id': id,
      'title': title,
      'thumbnail': thumbnail,
      'originPrice': originPrice,
      'price': price,
      'subTitle': subTitle,
      'salesTag': salesTag,
      'evaluate': evaluate,
      'favourable': favourable,
      'repeatCustomer': repeatCustomer,
      'score': score,
    };
  }

  factory GoodsModel.fromMap(Map<String, dynamic> map) {
    return GoodsModel(
      id: map['id'] as int,
      title: map['title'] as String,
      thumbnail: map['thumbnail'] as String,
      originPrice: map['originPrice'] as double,
      price: map['price'] as double,
      subTitle: map['subTitle'] as String,
      salesTag: map['salesTag'] != null
          ? List<String>.from((map['salesTag'] as List<String>))
          : null,
      evaluate: map['evaluate'] as int,
      favourable: map['favourable'] as int,
      repeatCustomer: map['repeatCustomer'] as int,
      score: map['score'] as double,
    );
  }

  String toJson() => json.encode(toMap());

  factory GoodsModel.fromJson(String source) =>
      GoodsModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  bool operator ==(covariant GoodsModel other) {
    if (identical(this, other)) return true;

    return other.id == id &&
        other.title == title &&
        other.thumbnail == thumbnail &&
        other.originPrice == originPrice &&
        other.price == price &&
        other.subTitle == subTitle &&
        listEquals(other.salesTag, salesTag) &&
        other.evaluate == evaluate &&
        other.favourable == favourable &&
        other.repeatCustomer == repeatCustomer &&
        other.score == score;
  }

  @override
  int get hashCode {
    return id.hashCode ^
        title.hashCode ^
        thumbnail.hashCode ^
        originPrice.hashCode ^
        price.hashCode ^
        subTitle.hashCode ^
        salesTag.hashCode ^
        evaluate.hashCode ^
        favourable.hashCode ^
        repeatCustomer.hashCode ^
        score.hashCode;
  }
}
